import storageService from "@api/storageService";
import { SET_USER_DATA, LOGOUT, LAST_SELECTED_YEAR } from './mutation-types'

const userModule = {
  namespaced: true,
  state: {
    user:  JSON.parse(storageService.get(storageService.USER_INFO) || "{}"),
    lastSelectedYear: storageService.get(storageService.LAST_SELECTED_YEAR) || ""
  },
  mutations: {
    [SET_USER_DATA] (state, data) {
      state.user = data;
      storageService.set(storageService.USER_INFO, JSON.stringify(state.user))
    },
    [LOGOUT] (state) {
      state.user = undefined;
      storageService.set(storageService.USER_INFO, "")
    },
    [LAST_SELECTED_YEAR] (state, data) {
      state.lastSelectedYear = data;
      storageService.set(storageService.LAST_SELECTED_YEAR, state.lastSelectedYear)
    },
  },
  getters: {
    token: state => {
      
      if (!state.user || !state.user.auth) {
        return ""
      }
      return state.user.auth.token || ""
    },
    userName: state => {
      if (!state.user || !state.user.user_info) {
        return ""
      }
      return state.user.user_info.real_name || ""
    }
  },
  actions: {

  }
}
export default userModule
